package util;

import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.request.base.LedForm;
import com.cloudbroker.bcs.csvc.lightinvesting.facade.service.aspect.UserCache;
import com.cloudbroker.bcs.platform.ltb.bizapi.constants.type.UserCacheFreshType;
import com.cloudbroker.bcs.platform.ltb.bizapi.constants.type.UserType;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.base.vo.BrokerVO;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.base.vo.UserVO;
import com.cloudbroker.bcs.platform.ltb.common.config.ConfigValues;
import com.cloudbroker.bcs.platform.ltb.common.util.CacheHelper;
import com.cloudbroker.bcs.platform.ltb.common.util.SessionHelper;
import com.hundsun.jres.common.util.UUID;

/**
 * Created by tianrui on 2016/7/8.
 * user_token 缓存数据 userInfo + user_key + op_entrust_way + op_station + terminal_type
 */
public class UserTokenUtil {

    private final static int baleFulCount = 4;
    private final static int frezzTime = 60;

    /**
     * 新增user_token缓存
     * */
    public static String addUserToken(UserCache userCache){        String user_token = UUID.randomUUID().toString();
        SessionHelper sessionHelper = SessionHelper.getInstance();
        sessionHelper.setInfoForSession(user_token, userCache.toStringInfo(),
                ConfigValues.getSessionTimeout());
        return user_token;
    }





    /**
     * 更新user_token缓存
     * */
    public static void updateUserToken(String user_token, UserCache userCache, int valid_time){
        SessionHelper sessionHelper = SessionHelper.getInstance();
        sessionHelper.setInfoForSession(user_token, userCache.toStringInfo(), valid_time);
    }



    /**
     * 查询user_token缓存
     * */
    public static UserCache queryUserToken(String user_token){
        CacheHelper cacheHelper = CacheHelper.getInstance();
        return UserCache.fromStringInfo(cacheHelper.get(user_token));
    }


    /**
     * 删除user_token缓存
     * */
    public static void deleteUserToken(String user_token){
        SessionHelper.getInstance().delete(user_token);
    }




    /**
     * 缓存后台参数设置
     * */
    public static <T extends LedForm> void setBGBrokerVO(BrokerVO bgBrokerVO, T form){
        bgBrokerVO.setCompany_id(form.getCompany_id());
        bgBrokerVO.setSendercomp_id(form.getSendercomp_id());
        bgBrokerVO.setOp_branch_no(form.getOp_branch_no());
        bgBrokerVO.setBranch_no(form.getBranch_no());
        bgBrokerVO.setPassword_type(form.getPassword_type());
        bgBrokerVO.setOperator_no(form.getOperator_no());
        bgBrokerVO.setOp_entrust_way(form.getOp_entrust_way());
        bgBrokerVO.setOp_station(form.getOp_station());
        bgBrokerVO.setUser_key(form.getUser_id());
    }





    /**
     * 用户登录缓存
     * key = company + sendercomp_id + mobile + user_type
     * */
    public static boolean updatedLoginUserToken(String key){
        String accountFrezzToken = "AccFrezz" + key;
        String user_token = "baleFul" + key;
        CacheHelper cacheHelper = CacheHelper.getInstance();
        if (cacheHelper.exists(accountFrezzToken)) return true;
        String loginCount = cacheHelper.get(user_token);
        if (loginCount != null){
            int count = Integer.valueOf(loginCount).intValue();
            count += 1;
            loginCount = String.valueOf(count);
        }else {
            loginCount = "1";
        }
        //有效时长1秒
        cacheHelper.set(user_token, loginCount, 1);
        //1秒之内请求超过3次视为恶意攻击
        if (Integer.valueOf(loginCount).intValue() > baleFulCount){
            //视为恶意后此账号封闭1分钟
            cacheHelper.set(accountFrezzToken, String.valueOf(loginCount), frezzTime);
            return true;
        }else {
            return false;
        }
    }
}
